using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class PlayerHPBar : MonoBehaviour
{
    public Image fastImage;
    public Image slowImage;

    private PlayerController controller;
    private bool startSlow;

    private void Start()
    {
        controller = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent<PlayerController>();
    }
    void Update()
    {
        fastImage.fillAmount = (float)(controller.StateData.CurHP)/(float)(controller.getMaxHP());
        if(fastImage.fillAmount>slowImage.fillAmount )
            slowImage.fillAmount = fastImage.fillAmount;
        else if(!startSlow)
            StartCoroutine(slowBar());
    }

    private IEnumerator slowBar()
    {
        startSlow = true;
        while(fastImage.fillAmount<slowImage.fillAmount)
        {
            slowImage.fillAmount = Mathf.Lerp(slowImage.fillAmount, fastImage.fillAmount, 0.05f);
            yield return null;
        }
        startSlow = false;
    }
}
